using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Collections;

namespace Faust.Andrew.MTG
{
    public static class DeckCardHelper
    {
        public static void Sort(ObservableCollection<DeckCard> cards, string sortBy)
        {
            List<DeckCard> cardList = new List<DeckCard>(cards);

            switch (sortBy)
            {
                case "Name":
                    cardList.Sort(NameComparer);
                    break;
                case "Color":
                    cardList.Sort(ColorComparer);
                    break;
                case "Rarity":
                    cardList.Sort(RarityComparer);
                    break;
                case "Type":
                    cardList.Sort(TypeComparer);
                    break;
                case "Converted Mana Cost":
                    cardList.Sort(CostComparer);
                    break;
                case "Owned":
                    cardList.Sort(OwnedComparer);
                    break;
                case "Quantity":
                    cardList.Sort(QuantityComparer);
                    break;
                default:
                    cardList.Sort(NameComparer);
                    break;
            }

            cards.Clear();
            foreach (DeckCard card in cardList)
            {
                cards.Add(card);
            }
        }

        public static int NameComparer(DeckCard x, DeckCard y)
        {
            if (x == null && y == null) { return 0; }
            if (x == null && y != null) { return -1; }
            if (x != null && y == null) { return 1; }

            return String.Compare(x.Card.Name, y.Card.Name);
        }

        public static int ColorComparer(DeckCard x, DeckCard y)
        {
            if (x == null && y == null) { return 0; }
            if (x == null && y != null) { return -1; }
            if (x != null && y == null) { return 1; }

            return String.Compare(x.Card.Color, y.Card.Color);
        }

        public static int RarityComparer(DeckCard x, DeckCard y)
        {
            if (x == null && y == null) { return 0; }
            if (x == null && y != null) { return -1; }
            if (x != null && y == null) { return 1; }

            int xVal = GetRarityValue(x.Card.Rarity);
            int yVal = GetRarityValue(y.Card.Rarity);

            //We want to sort by rarest to least rare so we need to reverse the order
            //Because the rarest have the largest value.
            return yVal.CompareTo(xVal);
        }

        private static int GetRarityValue(string rarity)
        {
            switch (rarity)
            {
                case "Mythic Rare":
                    return 6;
                case "Special":
                    return 5;
                case "Rare":
                    return 4;
                case "Uncommon":
                    return 3;
                case "Common":
                    return 2;
                case "Basic Land":
                    return 1;
                default:
                    return 0;

            }
        }

        public static int TypeComparer(DeckCard x, DeckCard y)
        {
            if (x == null && y == null) { return 0; }
            if (x == null && y != null) { return -1; }
            if (x != null && y == null) { return 1; }

            return String.Compare(x.Card.CardTypes, y.Card.CardTypes);
        }

        public static int OwnedComparer(DeckCard x, DeckCard y)
        {
            if (x == null && y == null) { return 0; }
            if (x == null && y != null) { return -1; }
            if (x != null && y == null) { return 1; }

            //We want to sort by most to least owned so we need to reverse the order
            //Because the the int comparer sorts in ascending order by default
            return y.Card.Quantity.CompareTo(x.Card.Quantity);
        }

        public static int QuantityComparer(DeckCard x, DeckCard y)
        {
            if (x == null && y == null) { return 0; }
            if (x == null && y != null) { return -1; }
            if (x != null && y == null) { return 1; }

            //We want to sort by most to least owned so we need to reverse the order
            //Because the the int comparer sorts in ascending order by default
            return y.Quantity.CompareTo(x.Quantity);
        }

        public static int CostComparer(DeckCard x, DeckCard y)
        {
            if (x == null && y == null) { return 0; }
            if (x == null && y != null) { return -1; }
            if (x != null && y == null) { return 1; }

            return x.Card.ConvertedManaCostNumeric.CompareTo(y.Card.ConvertedManaCostNumeric);
        }
    }
}
